# -*- coding: utf-8 -*-
##py2exe
#from Bauteilkiste.UIClasses import UIMainView
#from Bauteilkiste.Modules import Datenbank, Settings
##Normal
from Bauteilkiste.UIClasses import UIMainView
from Bauteilkiste.Modules import Settings, Datenbank
#from Settings import Settings
#from Datenbank import Datenbank
from PyQt4 import QtGui, QtCore

class Main(QtGui.QMainWindow):
    db = Datenbank.Datenbank()
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        
        self.s = Settings.Settings()
        self.mainView = UIMainView.UIMainView(self.s.settings["tables"])
        self.populateTables()           
        self.setCentralWidget(self.mainView)
        
        self.show()
        
        try:
            self.setGeometry(int(self.s.settings["windowPositionX"]), int(self.s.settings["windowPositionY"]), int(self.s.settings["windowWidth"]), int(self.s.settings["windowHeight"]))
        except:
            self.setWindowState(QtCore.Qt.WindowMaximized)
            
    def populateTables(self, resetTable = None):
        for table in self.mainView.tables:
            self.mainView.tables[table].populateTable()
            
    def closeEvent(self, event):
        self.s.settingsCursor.execute('UPDATE "main"."settings" SET value="'+str(self.height())+'" WHERE key="windowHeight"')
        self.s.settingsCursor.execute('UPDATE "main"."settings" SET value="'+str(self.width())+'" WHERE key="windowWidth"')
        self.s.settingsCursor.execute('UPDATE "main"."settings" SET value="'+str(self.pos().x())+'" WHERE key="windowPositionX"')
        self.s.settingsCursor.execute('UPDATE "main"."settings" SET value="'+str(self.pos().y())+'" WHERE key="windowPositionY"')
        self.s.settingsConnection.commit()
